package com.fw.application.security.comm;

import com.fw.enums.CodeStatus;
import com.fw.exception.CustomException;
import com.fw.system.web.model.entity.TbUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * 用户继承的属性
 * @Author:yanwei
 * @Date: 2020/10/14 - 17:05
 */


public class BaseUserDetails implements UserDetails {

/*    private Object user;
    public BaseUserDetails(Object user){
        this.user = user;
    }

    public Object getUser() {
        return user;
    }*/



    private TbUser user;

    public BaseUserDetails(TbUser user) {
        this.user = user;
    }


    public TbUser getUser() {
        if (Objects.isNull(user)) throw new CustomException(CodeStatus.SIGN_ERROR.getMessage());
        if (user.getIsState().equals(0)) throw new CustomException(CodeStatus.ACCOUNT_FREEZED.getMessage());
        return user;
    }

    public TbUser getDefaultUser() {
        return user;
    }



    /**
     * 返回用户角色&权限
     * @return
     *
     * 角色必須加 ROLE_ 前綴
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Arrays.asList("sss").stream()
                .filter(permission -> permission!=null)
                .map(permission ->new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return "$2a$10$0rfXlmjl68YWKS/bghBTEOPytuA0kH8vhkc2b2DaH1Mo9iV5mRQbi";
    }

    @Override
    public String getUsername() {
        return "yanwei";
    }

    @Override
    public boolean isAccountNonExpired() {
        return Boolean.TRUE;
    }

    @Override
    public boolean isAccountNonLocked() {
        return Boolean.TRUE;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return Boolean.TRUE;
    }

    /**
     * 是否禁用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return false;
    }
}
